package manager.msales;

import java.io.*;
import common.db.*;
import common.main.*;
import java.util.*;

public class SalesListBean extends MainBean
{
	public void funcExecute() throws Exception
	{


		String delivery_state = (String)inputTable.get("delivery_state");
		String age= (String)inputTable.get("age");
		String sex= (String)inputTable.get("sex");
		String productName= (String)inputTable.get("productName");

		String date1 = (String)inputTable.get("date1");
		String date2 = (String)inputTable.get("date2");
		String hidden = (String)inputTable.get("hidden");
		
		String inputPage = (String)inputTable.get("inputPage");
		
		queryBuffer = new StringBuffer("");
		queryBuffer.append("SELECT COUNT(SEQ) COUNT FROM MAGIC ");
		DBExecute dbe = new DBExecute(queryBuffer.toString());
		outputList = dbe.getDataList();

		Hashtable countTable = (Hashtable)outputList.get(0);
		int count = Integer.parseInt((String)countTable.get("COUNT"));
		
		queryBuffer = new StringBuffer("");
		queryBuffer.append("SELECT TO_CHAR(a.REG_DATE,  'YYYY-MM-DD') REG_DATE, b.ID, a.SEQ, b.SEX, c.NAME PRODUCT, TO_CHAR(SYSDATE, 'YYYY') - SUBSTR(b.JUMIN1, 0, 4)+1 AGE, b.SEX, a.NAME, a.PROFIT FROM ORDERLIST a, MEMBER b, GOODS c WHERE a.REG_ID = b.ID(+) AND c.SEQ(+) = a.CODE AND "); 
		
		
		if (hidden.equals("TODAY"))
		{
			queryBuffer.append("TO_DATE(a.REG_DATE, 'YYYY-MM-DD') = TO_DATE(SYSDATE,'YYYY-MM-DD' ) AND ");
		}
		else if (hidden.equals("MONTH1"))
		{
			queryBuffer.append(" TO_DATE( a.REG_DATE, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -1) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD') AND ");
		}
		else if (hidden.equals("MONTH2"))
		{
			queryBuffer.append(" TO_DATE( a.REG_DATE, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -2) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD') AND ");
		}
		else if (hidden.equals("MONTH3"))
		{
			queryBuffer.append(" TO_DATE( a.REG_DATE, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -3) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD') AND ");
		}
		else if (!date1.equals("") && !date2.equals(""))
		{
			queryBuffer.append("TO_CHAR(TIME, 'YYYY-MM-DD')>= '"+ date1+ "' AND TO_CHAR(TIME, 'YYYY-MM-DD')<='"+date2 +"' AND ");
		}

		String sexValue="";
		if (!sex.equals(""))
		{
				queryBuffer.append(" b.AGE = '"+ age+"'");
		}
		if (!sex.equals(""))
		{
			queryBuffer.append(" delivery_state = '"+ delivery_state+"' AND ");
		}
		if (!productName.equals(""))
		{
			queryBuffer.append(" c.SEQ= '"+ productName+"' AND ");
		}

		int temp = inputPage.length();
		String tempValue="1";
		for (int i=1;i<temp ; i++ )
		{
			tempValue += "0";
		}

		int inputPageNumber = Integer.parseInt(inputPage);

		int start= Integer.parseInt(tempValue);
		if (!tempValue.equals("1"))
		{
			start += 1;
		}
		int end = start+9;

		if (end > count)
		{
			end = count;
		}

		inputTable.put("start", start);
		inputTable.put("end", end);

		queryBuffer.append("AND  ROWNUM BETWEEN '"+start+"' AND '"+end+ "' ");
		dbe = new DBExecute(queryBuffer.toString().substring(0, queryBuffer.lastIndexOf("AND ")));


		outputList = dbe.getDataList();
	}
}